package com.kc.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.kc.business.domain.BizBusinessCoupon;
import com.kc.business.domain.BizUserBusinessCoupon;
import com.kc.business.mapper.BizBusinessCouponMapper;
import com.kc.business.service.IBizBusinessCouponSerrvice;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商家优惠券service 实现类
 */
@Service
public class BizBusinessCouponSerrviceImpl implements IBizBusinessCouponSerrvice {
    @Resource
    private BizBusinessCouponMapper bizBusinessCouponMapper;
//    @Resource
//    private KcUserBusinessCouponMapper kcUserBusinessCouponMapper;
    /**
     * 优惠券列表
     * @param businessId
     * @return
     */
    @Override
    public List<BizBusinessCoupon> getCouponList(Integer businessId, Integer status) {
        //状态:1可领取，2未开始，3已过期，4已领完，5已删除
        String currDate = DateUtil.now();
        Integer isDeleted = 0;
        if(new Integer(5).equals(status)){
            isDeleted = 1;
        }
        List<BizBusinessCoupon> kcBusinessCouponList = bizBusinessCouponMapper.getCouponList(businessId,status,currDate,isDeleted);
        kcBusinessCouponList.forEach(kcBusinessCoupon -> {
            kcBusinessCoupon.setEndTime(kcBusinessCoupon.getEndTime().substring(0,10));
            kcBusinessCoupon.setStartTime(kcBusinessCoupon.getStartTime().substring(0,10));
        });
        return kcBusinessCouponList;

    }
    /**
     * 优惠券详情
     * @param couponId
     * @return
     */
    @Override
    public BizBusinessCoupon getCouponInfo(Integer couponId) {
        BizBusinessCoupon bizBusinessCoupon = new BizBusinessCoupon();
        bizBusinessCoupon.setId(couponId);
        return bizBusinessCouponMapper.selectByPrimaryKey(bizBusinessCoupon);
    }
    /**
     * 优惠券信息变更
     * @param bizBusinessCoupon
     * @return
     */
    @Override
    public Integer updateCoupon(BizBusinessCoupon bizBusinessCoupon) {
        return bizBusinessCouponMapper.updateByPrimaryKey(bizBusinessCoupon);
    }

    /**
     * 优惠券新增
     * @param bizBusinessCoupon
     * @return
     */
    @Override
    public Integer addCoupon(BizBusinessCoupon bizBusinessCoupon) {
        return bizBusinessCouponMapper.insert(bizBusinessCoupon);
    }

    /**
     * 获取优惠券领取记录
     * @param couponId
     * @return
     */
    @Override
    public List<BizUserBusinessCoupon> getCouponUserList(Integer couponId) {
        List<BizUserBusinessCoupon> kcUserBusinessCouponList = bizBusinessCouponMapper.getUserCouponList(couponId);
        String currDate = DateUtil.now();
        kcUserBusinessCouponList.forEach(kcUserBusinessCoupon -> {
            if(kcUserBusinessCoupon.getStatus() == 1){
                kcUserBusinessCoupon.setStatusName("已使用");
            }else{
                kcUserBusinessCoupon.setStatusName("未使用");
            }
            if(currDate.compareTo(kcUserBusinessCoupon.getEndTime())<1){
                kcUserBusinessCoupon.setStatusName("已过期");
                kcUserBusinessCoupon.setStatus(-1);
            }

        });
        return kcUserBusinessCouponList;
    }
}
